//
//  FriendsViewController.m
//  AgriMobile
//
//  Created by 春名 啓作 on 11/05/22.
//  Copyright 2011 e-agri. All rights reserved.
//

#import "FriendComViewController.h"
#define C_title		0
#define C_url		1
#define C_images	2

@implementation FriendComViewController

@synthesize friedComTableView;
@synthesize actionId;
@synthesize dataList;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

	NSLog(@"FriendComView - didLoad");

	SharedDictionary *sd = [SharedDictionary sharedDictionary];
	if ([actionId isEqualToString:@"a=page_fh_friend_list"]) {
		dataList = [[[sd.data objectForKey:@"friends"] objectForKey:@"data"] retain];
	}else if ([actionId isEqualToString:@"a=page_fh_com_list"]) {
		dataList = [[[sd.data objectForKey:@"communities"] objectForKey:@"data"] retain];
	}else if ([actionId isEqualToString:@"a=page_fh_event_list"]) {
		dataList = [[[sd.data objectForKey:@"events"] objectForKey:@"data"] retain];
	}else if ([actionId isEqualToString:@"a=page_fh_album_list"]) {
		dataList = [[[sd.data objectForKey:@"album"] objectForKey:@"data"] retain];
	}else{
		//reloadData
		NSString *urlstr = [NSString stringWithFormat:@"%@/wap?%@", URLRoot, actionId];
		NSDictionary *map = [[[[[HttpMethod alloc] autorelease] doGet:urlstr] JSONValue] retain];
		dataList = [[map objectForKey:@"data"] retain];
		[map release];
	}

	NSLog(@"map = %d entries %@", [dataList count], dataList);
	NSLog(@"FriendComView didLoad done");
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dataList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
	NSLog(@"in section %d row %d", indexPath.section, indexPath.row);
	NSArray *itemData = [dataList objectAtIndex:indexPath.row];
	//NSLog(@"itemData = %@", itemData);
	NSString *imgUrl = [NSString stringWithFormat:@"%@",[[itemData objectAtIndex:C_images] objectAtIndex:0]];
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	cell.textLabel.text = [itemData objectAtIndex:C_title];

	NSLog(@"imgUrl = %@", imgUrl);
	NSURL *url = [NSURL URLWithString:imgUrl];
	UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
	cell.imageView.image = img;
	[img release];

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.

	//NSLog(@"FriendComView - didSelectRowAtIndexPath");

	NSArray *itemData = [dataList objectAtIndex:indexPath.row];
	//NSLog(@"itemData = %@", itemData);
	
	if ([actionId rangeOfString:@"a=page_fh_album_list"].location != NSNotFound) {
		TopicViewController* topicViewController = [[TopicViewController alloc] initWithNibName:@"TopicView" bundle:nil];
		
		topicViewController.topicId = [itemData objectAtIndex:C_url];
		topicViewController.parentId = self.actionId;
		topicViewController.title = [itemData objectAtIndex:C_title];
		
		[self.navigationController pushViewController:topicViewController animated:YES];
		[topicViewController release];
	}else{
		TopicListViewController* topicListViewController = [[TopicListViewController alloc] initWithNibName:@"TopicListView" bundle:nil];
		topicListViewController.topicListUrl = [itemData objectAtIndex:C_url];
		topicListViewController.title = [itemData objectAtIndex:C_title];
		topicListViewController.topicListImgUrl = nil;
		if ([[itemData objectAtIndex:C_images] count] > 0)
			topicListViewController.topicListImgUrl = [[itemData objectAtIndex:C_images] objectAtIndex:0];
		[self.navigationController pushViewController:topicListViewController animated:YES];
		[topicListViewController release];
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

	NSLog(@"FriendComView - didReceiveMemoryWarning");
	
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
	
	NSLog(@"FriendComView - viewDidUnload");
}


- (void)dealloc {
    [super dealloc];
	NSLog(@"FriendComView - dealloc");
}


@end

